package com.salas.admin;

import java.util.List;
import javax.ejb.Remove;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import static org.jboss.seam.ScopeType.CONVERSATION;
//import static org.jboss.seam.ScopeType.SESSION;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import com.salas.entity.Order;
/**
 * @author qiaoguangwei
 * 
 *
 */
@Scope(CONVERSATION)
@Name("ship")
public class ShipAction implements Ship {
	
	@In
	private EntityManager entityManager;
   
	@DataModel
	private List<Order> orders1=null;
	
	@Out(required=false, scope=ScopeType.SESSION)
	private boolean isEmpty = true;
	
	@DataModelSelection
	@Out(required=false)
	private Order order;
	
	

	@Begin(join = true)
	public String select(){
		return "order_selected";
	}
	
	
	@Factory("orders1")
	public void getOrders1(){
		
		Query q = entityManager.createQuery(
		"from Order where status = :status").setParameter("status","OPEN");
		
		orders1= q.getResultList();
		
		if (orders1.size()!=0){
			isEmpty = false;
		}
		
	}
	
	@End
    public String ship()
    {
		order.setStatus("SHIPPED");    
		entityManager.merge(order);		
		orders1.remove(order);
		return "shipped";
    }
    
    @Destroy
    @Remove 
    public void destroy(){  	
    }
    
}
